package com.greenline.push.home.controllers.task;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.greenline.guser.annotation.GuserResource;
import com.greenline.push.common.json.BaseJson;
import com.greenline.push.common.json.GridJsonData;
import com.greenline.push.core.enums.TaskStatus;
import com.greenline.push.core.pushcenter.TaskSyncManager;
import com.greenline.push.dal.pushcenter.dataobject.TaskSyncDO;
import com.greenline.push.dal.query.TaskSyncQuery;

@Controller
@RequestMapping(value = "/task")
public class TaskSyncController {

    @Resource
    private TaskSyncManager taskSyncManager;
    private final Log logger = LogFactory.getLog(TaskSyncController.class);

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(ModelMap model) {
        return "task/index";
    }

    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public String queryRecord(ModelMap model) {
        return "task/index";
    }

    @RequestMapping("/query.json")
    @ResponseBody
    public GridJsonData<TaskSyncDO> listRecord(TaskSyncQuery query,
            @RequestParam(value = "page", defaultValue = "1", required = false) String pageIndex,
            @RequestParam(value = "pageSize", defaultValue = "10", required = false) String pageSize) {
        query.setPageIndex(Integer.parseInt(pageIndex));
        query.setPageSize(Integer.parseInt(pageSize));
        GridJsonData<TaskSyncDO> ret = new GridJsonData<TaskSyncDO>();
        ret.setQuery(taskSyncManager.listByQuery(query));
        return ret;
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String addInit(ModelMap model) {
        return "task/add";
    }

    @RequestMapping(value = "/add.json", method = RequestMethod.POST)
    public @ResponseBody
    BaseJson add(ModelMap model, @ModelAttribute("task") TaskSyncDO task) {
        BaseJson json = new BaseJson();
        if (task.getRunPeriod() == null) {
            task.setRunPeriod(60);
        }
        try {
            task.setStatus(TaskStatus.END.getStatus());
            task.setLastRecordId(0l);
            if (taskSyncManager.insert(task) > 0) {
                json.setMessage("提交成功！");
            } else {
                json.setHasError(true);
                json.setMessage("提交出错");
            }
        } catch (Exception e) {
            json.setHasError(true);
            json.setMessage("提交出错");
            logger.error(json.getMessage(), e);
        }
        return json;
    }

    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String updateInit(ModelMap model, @PathVariable Long id) {
        TaskSyncDO object = taskSyncManager.getById(id);
        model.put("task", object);
        return "task/update";
    }

    @RequestMapping(value = "/update.json", method = RequestMethod.POST)
    public @ResponseBody
    BaseJson update(ModelMap model, @ModelAttribute("taskSync") TaskSyncDO taskSync) {
        BaseJson json = new BaseJson();
        try {
            if (taskSyncManager.update(taskSync)) {
                json.setMessage("提交成功！");
            } else {
                json.setHasError(true);
                json.setMessage("提交出错");
            }
        } catch (Exception e) {
            json.setHasError(true);
            json.setMessage("提交出错");
            logger.error(json.getMessage(), e);
        }
        return json;
    }

    @GuserResource("消息模板删除")
    @RequestMapping(value = "/delete.json", method = RequestMethod.POST)
    public @ResponseBody
    BaseJson delete(ModelMap model, @ModelAttribute("id") Long id) {
        BaseJson json = new BaseJson();
        try {
            if (taskSyncManager.delete(id)) {
                json.setMessage("删除成功！");
            } else {
                json.setHasError(true);
                json.setMessage("删除出错");
            }
        } catch (Exception e) {
            json.setHasError(true);
            json.setMessage("删除出错");
            logger.error(json.getMessage(), e);
        }
        return json;
    }
}